package com.ithe.canteen.service.impl;

import com.ithe.canteen.mapper.FoodMapper;
import com.ithe.canteen.pojo.Food;
import com.ithe.canteen.service.FoodService;
import com.ithe.canteen.util.CreatIDUtil;
import com.ithe.canteen.util.UrlToUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Value("${file.url}")
    private String fileUrl;

    @Override
    public Food getFoodById(String foodID) {
        return foodMapper.getFoodById(foodID);
    }

    @Override
    public List<Food> queryFood(String foodName, String foodType) {
        List<Food> foodList = foodMapper.queryFood(foodName, foodType);
        for (Food food : foodList) {
            String imagePath = fileUrl + food.getFoodImage();
            food.setFoodImage(imagePath);
        }
        return foodList;
    }

    @Override
    public List<Food> recommendFood(String loveType, String hateType) {
        List<Food> foodList = foodMapper.recommendFood(loveType, hateType);
        for (Food food : foodList) {
            String imagePath = fileUrl + food.getFoodImage();
            food.setFoodImage(imagePath);
        }
        return foodList;
    }

    @Override
    public boolean managerAddFood(String foodImage, String foodName, String foodType, float foodPrice) {
        String foodID = CreatIDUtil.createID("food");
        return foodMapper.managerAddFood(foodID, UrlToUtil.extractFileNameFromUrl(foodImage), foodName, foodType, 8, foodPrice);
    }

    @Override
    public boolean managerDelFood(String foodID) {
        return foodMapper.managerDelFood(foodID);
    }
}
